<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小西瓜 <751707632@qq.com.com>
// +----------------------------------------------------------------------
namespace app\api\model;

use think\Model;
use think\Db;

class ImsJoinLogModel extends Model
{
    /**
     * 查询参会记录
     * @param [array] $[where] [<查询条件>]
     * @param [datatime] $[start_time] [<开始时间>]
     * @param [int] $[duration] [<时长>]
     * @return array
     */
    public function joinLogShow($where=[], $start_time, $duration)
    {
        $info = $this->where($where)->field('join_way, join_time, quit_time')->find();

        if ( $info ) {
            $info['join_way_name'] = joinWay()[$info['join_way']];
            if (  $info['join_time'] > $start_time ) {
                $info['join_status'] = meetingJoinStatus()[3];
            } else {
                $info['join_status'] = meetingJoinStatus()[2];
            }
            if ( $info['quit_time'] ) {
                $start_time = strtotime($start_time);
                $quit_time = strtotime($info['quit_time']);
                if ( ($quit_time-$start_time) < ($duration * 60) ) {
                    $info['join_status'] = meetingJoinStatus()[4];
                } else {
                    $info['join_status'] = meetingJoinStatus()[5];
                }
            }
        } else {
            $info['join_status'] =  meetingJoinStatus()[1];
        }
        return $info;
    }

    /**
     * [set 入场、离场]
     * @param [type] $data [description]
     */
    public function set($data){

        if ( empty($data) ) {
            return ['code'=>'0', 'msg'=>'缺少参数'];
        }
        $data_new = array();
        if(isset($data['vm_id']))     $data_new['vm_id']    = $data['vm_id'];
        if(isset($data['join_uid']))  $data_new['join_uid'] = $data['join_uid'];
        if(isset($data['join_way']))  $data_new['join_way'] = $data['join_way'];
        if(isset($data['is_vm']))     $data_new['is_vm']    = $data['is_vm'];

        // 判断是还是修改
        if(!empty($data['id']) && ($id = intval(trim($data['id'])))) {
            $data_new['quit_time'] = date('Y-m-d H:i:s', time());
            $this->where('id', $id)->update($data_new);
            return true;
        } else {
            $data_new['join_time'] = date('Y-m-d H:i:s', time());
            $id = $this->insert($data_new);
            return true;
        }
    }

    /**
     * 查询参会列表
     * @param [array] $[where] [<查询条件>]
     * @param [string] $[field] [<查询字段>]
     * @return array
     */
    public function joinLogList($where=[], $field='im.staff_number,im.staff_name,im.staff_pic')
    {
        $list = $this->alias('jl')
        ->join('__IMS_MEMBER__ im','jl.join_uid = im.id', 'left')
        ->field($field)->order('jl.id desc')
        ->where($where)->select()->toArray();

        foreach ( $list as $key => $val ) {
            if ( $val['staff_pic'] ) $list[$key]['staff_pic'] = cmf_get_image_url(str_replace("\\","/", $val['staff_pic']));
        }

        return $list;
    }
}